﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace Test
{
    public partial class Form3 : Form
    {
        private int a;


        public Form3()
        {
            InitializeComponent();
        }

     
        private void Form3_Load(object sender, EventArgs e)
        {
            chart1.ChartAreas["ChartArea1"].AxisX.ScaleView.Size = 10;//x坐标显示的个数------------控制这个数量的大小进行缩放     
            chart1.ChartAreas[0].AxisX.LabelStyle.IntervalType = DateTimeIntervalType.Seconds;//设置x轴间隔值单位：秒
            chart1.ChartAreas[0].AxisX.LabelStyle.Interval = 1;//设置X轴的值的间隔大小
            chart1.ChartAreas[0].AxisX.LabelStyle.IsEndLabelVisible = false;//是否在轴末尾显示标记
            chart1.ChartAreas[0].AxisX.LabelStyle.Format = "HH:mm:ss.ff";//设置X轴的数据样式
            chart1.ChartAreas[0].AxisX.ScaleView.MinSizeType = DateTimeIntervalType.Seconds;
            chart1.ChartAreas[0].AxisX.ScaleView.SizeType = DateTimeIntervalType.Seconds; //度量单位
            chart1.ChartAreas[0].AxisX.ScaleView.SmallScrollMinSize = 1;
            chart1.ChartAreas[0].AxisX.ScaleView.SmallScrollMinSizeType = DateTimeIntervalType.Seconds;
            chart1.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Seconds;
            chart1.ChartAreas[0].AxisX.Enabled = AxisEnabled.True;//将X轴始终展示
            chart1.ChartAreas[0].AxisY.Enabled = AxisEnabled.True;//将Y轴始终展示
            chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.Gray;//设置X轴网格线颜色
            chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.Gray;//设置Y轴网格线颜色
            chart1.ChartAreas[0].AxisX.ScrollBar.Enabled = true;//关闭系统的滚动条，也可以不关闭，就可以滑动
            chart1.ChartAreas[0].AxisY.ScrollBar.Enabled = true;//关闭系统的滚动条，也可以不关闭，就可以滑动
            chart1.Series[0].BorderWidth = 2;//线宽
            chart1.Series[1].BorderWidth = 2;//线宽
            chart1.Series[2].BorderWidth = 2;//线宽
            chart1.Series[3].BorderWidth = 2;//线宽
            chart1.Series[4].BorderWidth = 2;//线宽

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            a++;
            float[] data = new float[1000];
            float[] data1 = new float[1000];
            float[] data2 = new float[1000];
            float[] data3 = new float[1000];
            
            for (int i = 0; i < 1000; i++)
            {
                data[i]  = (float)(0.1 * i);
                data1[i] = (float)Math.Sin(data[i]);
                data2[i] = 2 * (float)Math.Sin(data[i]);
                data3[i] = 5 * (float)Math.Sin(data[i]);
            }
            List<float[]> dataList = new List<float[]>();
            dataList.Add(data1);
            dataList.Add(data2);
            dataList.Add(data3);
            int count = dataList.Count;
            float[] res = new float[dataList[0].Length];

            //线性平均
            //for (int i = 0; i < res.Length; i++)
            //{
            //    double temp = 0;
            //    for (int j = 0; j < count; j++)
            //    {
            //        temp += dataList[j][i];
            //    }
            //    res[i] = (float)(temp / count);
            //}

            //稳态平均
            //for (int i = 0; i < res.Length; i++)
            //{
            //    float amp = 0;
            //    for (int j = 0; j < count; j++)
            //    {
            //        amp = (count - 1) * amp / count + dataList[j][i] / count;
            //    }
            //    res[i] = amp;
            //}

            //指数平均
            int t = 2;//常数具体值未知
            for (int i = 0; i < res.Length; i++)
            {
                float amp = 0;
                for (int j = 0; j < count; j++)
                {
                    amp = (t - 1) * amp / t + dataList[j][i] / t;
                }
                res[i] = amp;
            }

            chart1.Series[0].Points.AddXY(DateTime.Now, data1[a]);
            chart1.Series[1].Points.AddXY(DateTime.Now, data2[a]);
            chart1.Series[2].Points.AddXY(DateTime.Now, data3[a]);
            chart1.Series[3].Points.AddXY(DateTime.Now, res[a]);
            if (chart1.ChartAreas[0].AxisX.ScaleView.Size > 0)
            {
                chart1.ChartAreas[0].AxisX.ScaleView.Scroll(System.Windows.Forms.DataVisualization.Charting.ScrollType.Last);
            }

        }
        private void BT_Start_Click(object sender, EventArgs e)
        {
            timer1.Enabled = !timer1.Enabled;
        }
        private void chart1_Click(object sender, EventArgs e)
        {
            
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void TB_TargetValue_TextChanged(object sender, EventArgs e)
        {

        }

        private void TB_LowerLimitValue_TextChanged(object sender, EventArgs e)
        {

        }

        private void Form3_Load_1(object sender, EventArgs e)
        {

        }
    }
}
